<?php
class ZMobilesignup
{
	static public function Create( $mobile, $secret=null ) 
	{
		if (!Utility::IsMobile($mobile, true)) return;
		$secret = $secret ? $secret : Utility::VerifyCode();

		$table = new Table('sms_verify', array(
				'mobile' => $mobile,
				'mobilecode' => $secret,
		));
		
		$condition = array( 
					'mobile' => $mobile,
		);
		$haveone = DB::GetTableRow('user', $condition);
        if ($haveone){
		 	return false;
		}
		
		$havecode = DB::GetTableRow('sms_verify', $condition);
		if( $havecode ){
			return DB::Update('sms_verify', $havecode['id'] ,array(
					'mobilecode' => $secret,
					'create_time' => time(),
			));
		}else{
			return DB::Insert('sms_verify', array(
					'mobile' => $mobile,
					'mobilecode' => $secret,
					'create_time' => time(),
			));
		}
		//$have = Table::Fetch('sms_verify', $mobile, 'mobile');
		//if ( $have ) {
		//	return true;
		//}
		//return $sms_id;
	}

	static public function Enable($mobile, $enable=false,$user_id='') {
        $condition = array( 
		      'tools' => $mobile, 
		      'enable' => 'Y',
		);
		$remove = DB::GetTableRow('toolsbind', $condition);
                if ($remove){
			Table::Delete('toolsbind', $remove['id']);
		}
        $havecondition = array( 
		      'user_id' => $user_id, 
		      'enable' => 'Y',
		);
		$removeold = DB::GetTableRow('toolsbind', $havecondition);
                if ($removeold){
			Table::Delete('toolsbind', $removeold['id']);
		}

		$sms = Table::Fetch('toolsbind', $mobile, 'tools');
                $time = time();
		if ( $sms ) {
			Table::UpdateCache('toolsbind', $sms['id'], array(
				'enable' => 'Y',
                'create_time' => $time,
			));
                Table::UpdateCache('user', $sms['user_id'], array(
				'mobile' => $mobile,
				'mobilecode' => 'yes',
                'enable' => 'Y',
			));
		} 
	}


}
